package com.kou.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author SteinY
 * @create 2022-03-05 15:56
 * @description:
 */
public class DataSourceUtil {
    public  static DruidDataSource dataSource;
    public  static  ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    static {
        try {
            Properties properties = new Properties();
            InputStream resourceAsStream = DruidDataSource.class.getClassLoader().getResourceAsStream("druid.properties");
            properties.load(resourceAsStream);
            resourceAsStream.close();
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);

        } catch (Exception e) {
            throw new RuntimeException("获取连接失败！");
        }

    }

    public DruidDataSource getDataSource(){
        return dataSource;
    }

    public static void closeAll(ResultSet resultSet, Statement statement, Connection connection){
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                if (connection.getAutoCommit()){
                    connection.close();
                    threadLocal.remove();
                }
            }
        }catch (Exception e){

        }
    }

    //设置手动处理事务的连接获取与返还
    public static Connection getConnection(){
        Connection connection =threadLocal.get();
        try {
            if(connection==null){
                connection = dataSource.getConnection();
                threadLocal.set(connection);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static void begin(){
        Connection connection = getConnection();
        if (connection!=null){
            try {
                connection.setAutoCommit(false);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static  void commit(){
        Connection connection = getConnection();
        if (connection!=null){
            try {
                connection.commit();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static  void rollback(){
        Connection connection = getConnection();
        if (connection!=null){
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static  void close(){
        Connection connection = getConnection();
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }


}
